package com.yinile.zc04.schedule;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yinile.zc04.domain.Author;

@Service
public class ScheduledTaskService {
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
	
//	@Scheduled(fixedRate=5000)
//	public void reportCurrentTime() {
//		System.out.println(dateFormat.format(new Date()));
//	}
	
	@Scheduled(fixedRate=60000)
	public void reportCurrentTime() {
		RestTemplate restTemplate = new RestTemplate();
        String a = restTemplate.postForObject("http://localhost:8080/author/test/27634033-e3c7-483d-a6e1-0749b57361e8",null, String.class );
		ObjectMapper mapper = new ObjectMapper();
		Map<?,?> map = null;
		try {
			map = mapper.readValue(a, Map.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		System.out.println(dateFormat.format(new Date()) + "  " + a + "   " + map.get("code"));
	}
	
	@Scheduled(fixedRate=30000)
	public void reportCurrentTime2() {
		RestTemplate restTemplate = new RestTemplate();
		Author author = new Author();
		author.name = "元四";
		author.address = "深圳路11号";
		author.balance = new BigDecimal("0");
		author.level = 1;
		author.isVip = true;
		author.birthday = new Timestamp(System.currentTimeMillis());
        String a = restTemplate.postForObject("http://localhost:8080/author", author, String.class );
		ObjectMapper mapper = new ObjectMapper();
		Map<?,?> map = null;
		try {
			map = mapper.readValue(a, Map.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		System.out.println("22222222  " + dateFormat.format(new Date()) + "  " + a + "   " + map.get("code"));
	}
}
